package router

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"
	"strconv"
	"user-center/src/api"
	"user-center/src/config"
	"user-center/src/constant"
	"user-center/src/utils"
)

func InitRouter() {
	gin.SetMode(gin.ReleaseMode)

	router := gin.Default()
	// 该死，要设置在路由前面
	store, err := redis.NewStore(10, "tcp", config.GetGlobalConfig().RedisConfig.Url, "", []byte("secret"))
	if err != nil {
		log.Error(err)
		panic("redis error ")
	}
	// store.Options(sessions.Options{
	// 	Path:     "/",
	// 	MaxAge:   constant.CookieExpire,
	// 	Secure:   true,
	// 	SameSite: 4,
	// })
	router.Use(sessions.Sessions(constant.MySession, store))
	router.Use(utils.Cors())

	userGroup := router.Group("/api/user")
	{
		userGroup.POST("register", api.UserRegister)
		userGroup.POST("login", api.UserLogin)
		userGroup.POST("logout", api.UserLogout)
		userGroup.POST("update", api.UserUpdate)
		userGroup.POST("delete", api.UserDelete)

		userGroup.GET("info", api.UserInfo)
		userGroup.GET("current", api.UserGetCurrent)
		userGroup.GET("search", api.UserSearch)
		userGroup.GET("search/tags", api.UserSearchTags)
		userGroup.GET("recommend", api.UserRecommend)
		userGroup.GET("match", api.UserMatch)
	}

	teamGroup := router.Group("/api/team")
	{
		teamGroup.POST("add", api.TeamAdd)
		teamGroup.POST("update", api.TeamUpdate)
		teamGroup.POST("join", api.TeamJoin)
		teamGroup.POST("quit", api.TeamQuit)
		teamGroup.POST("delete", api.TeamDelete)

		teamGroup.GET("list/my/create", api.TeamListsByMyCreated)
		teamGroup.GET("list/my/join", api.TeamListJoinInMy)
		teamGroup.GET("get", api.TeamGetById)
		teamGroup.GET("list", api.TeamLists)
		teamGroup.GET("list/page", api.TeamListsByPage)
	}

	friendGroup := router.Group("/api/friend")
	{
		friendGroup.POST("add", api.FriendAdd)
		friendGroup.POST("delete", api.FriendDelete)
		friendGroup.POST("chat/sendMessage", api.FriendChatAdd)

		friendGroup.GET("chat/list", api.FriendChatList)
		friendGroup.GET("chat/history", api.FriendChatHistory)
	}

	groupGroup := router.Group("/api/group")
	{
		groupGroup.POST("chat/sendMessage", api.GroupChatAdd)

		groupGroup.GET("member/list", api.GroupMemberList)
		groupGroup.GET("info", api.TeamGetById)
		groupGroup.GET("chat/list", api.GroupChatList)
		groupGroup.GET("chat/history", api.GroupChatHistory)

	}

	taskGroup := router.Group("/api/task")
	{
		taskGroup.POST("add", api.TaskAdd)
		taskGroup.POST("change", api.TaskChange)

		taskGroup.GET("all", api.TaskAll)
		taskGroup.GET("info", api.TaskInfo)
		taskGroup.GET("my", api.TaskByUser)

	}

	router.GET("/api/code/ws", api.CodeUpgrader)
	router.GET("/api/ai/code/ws", api.AiCodeUpgrader)

	router.GET("/api/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))
	err = router.Run(":" + strconv.Itoa(config.GetGlobalConfig().SvrConfig.Port))
	if err != nil {
		log.Error("start server err:" + err.Error())
	}
}
